package com.taotao.slink.controller;

import com.taotao.common.Pagination;
import com.taotao.common.RespData;
import com.taotao.common.auth.AuthRequired;
import com.taotao.common.auth.CurrUserId;
import com.taotao.slink.model.dto.Link;
import com.taotao.slink.service.ShortLinkService;
import com.taotao.sys.model.vo.sl.ShortLinkPageParam;
import com.taotao.sys.model.vo.sl.ShortLinkPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Slf4j
@Api(tags = "短链接接口")
@RestController
@RequestMapping("/sl")
public class ShortLinkController {

    @Autowired
    private ShortLinkService shortLinkService;


    @AuthRequired
    @ApiOperation(value = "分页列表", notes = "")
    @PostMapping(value = "/pageList")
    public RespData<Pagination<ShortLinkPageVO>> pageList(@RequestBody ShortLinkPageParam param) {

        return RespData.successResp(shortLinkService.pageList(param));
    }

    @AuthRequired
    @ApiOperation(value = "添加短链接", notes = "")
    @PostMapping(value = "/add")
    public RespData<String> add(@Valid @RequestBody Link link, @CurrUserId Long userId) {

        String shortLink = shortLinkService.addShortLink(link, userId);

        return RespData.successResp(shortLink);
    }


    @AuthRequired
    @ApiOperation(value = "删除短链接", notes = "")
    @PostMapping(value = "/del")
    public RespData<String> del(@Valid @NotEmpty(message = "id不能为空") @ApiParam(value = "id列表", required = true) @RequestBody Set<Long> ids, @CurrUserId Long userId) {

        shortLinkService.delShortLink(ids, userId);

        return RespData.successResp();
    }
}
